# @Version : 1.0
# @Author  : wxz
# @File    : mysql_client.py
# @Time    : 2025/5/30 15:06
# @Describe: 描述...
from sqlmodel import create_engine, Session

from config import app_settings


class MySQLClient:

    username = app_settings.mysql.username
    password = app_settings.mysql.password
    host = app_settings.mysql.host  # 例如：'localhost' 或 '127.0.0.1'
    port = app_settings.mysql.port  # 通常是 3306
    database = app_settings.mysql.database_name
    dbHost = f'mysql+pymysql://{username}:{password}@{host}:{port}/{database}'

    engine = create_engine(
        dbHost,
        echo=app_settings.mysql.echo_sql,  # 是否打印SQL
        pool_size=app_settings.mysql.pool_size,  # 连接池的大小，指定同时在连接池中保持的数据库连接数，默认:5
        max_overflow=app_settings.mysql.max_overflow,  # 超出连接池大小的连接数，超过这个数量的连接将被丢弃,默认: 5
        pool_timeout=app_settings.mysql.pool_timeout,
        pool_recycle=app_settings.mysql.pool_recycle,
        pool_pre_ping=app_settings.mysql.pool_pre_ping,
    )

    @classmethod
    def get_db_session(cls,)-> Session:

        with Session(cls.engine) as session:
            yield session